class Todo < ActiveRecord::Base
  belongs_to :project
  belongs_to :milestone
  has_many   :todo_items, :conditions => "finished_at is NULL or finished_at = 0", :order => 'id DESC', :dependent => :destroy 
  has_many   :my_todo_items, :class_name=>'TodoItem', :conditions => ["(user_id = 0 or user_id = ?) and (finished_at is NULL or finished_at = 0)",Thread.current[:user].id], :order => 'id DESC'
             
  has_many   :finished_todo_items, :class_name => "TodoItem",
	     :conditions => "finished_at > 0", :order => 'finished_at DESC',
	     :dependent=>:destroy

  after_create :save_log
def save_log
  Log.info(self.project,
       "<a href=/projects/#{self.project_id}/todos>#{self.name}</a>",
       2,
       "Posted by",
       Thread.current[:user]
    )
end
def started_at
  if self.milestone_id > 0
    return self.milestone.started_at
  elsif self.todo_items.size > 0
    return TodoItem.find_by_sql("select min(start_at) as start_at from todo_items where todo_id =#{self.id}").first.start_at.to_time
  else
    self.created_at
  end
end
end
